package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.ReportBean;
import com.ray.bean.TableData;
import com.ray.exception.ServiceException;
import com.ray.service.ApprovalService;
import com.ray.service.CompanyMaterialService;
import com.ray.service.OrderService;
import com.ray.service.OtherWarehouseService;
import com.ray.service.PurchaseOrderService;
import com.ray.service.WorkshopService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

@Controller
@RequestMapping("/approval")
public class ApprovalController {
	
	@Autowired
	ApprovalService approvalService;
	@Autowired
	WorkshopService workshopService;
	@Autowired
	OrderService orderService;
	@Autowired
	CompanyMaterialService companyMaterialService;
	@Autowired
	OtherWarehouseService OtherWarehouseService;
	@Autowired
	PurchaseOrderService PurchaseOrderService;
	
	//获取未审批的报工数据
	@RequestMapping("/getRoportNotApprovalData")
	@ResponseBody
	public DatatablesViewPageBean getRoportNotApprovalData(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getRoportNotApprovalData(map);
	}
	
	
	//获取未审批的报工数据
	@RequestMapping("/downloadReportData")
	@ResponseBody
	public void downloadReportData(HttpServletRequest request,HttpServletResponse response) throws ServiceException, IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
		Map map=RequestUtil.getRequestAllParam(request);
		map.put("start", null);
		map.put("length", null);
		DatatablesViewPageBean<ReportBean> datatablesViewPageBean=workshopService.getRoportNotApprovalData(map);
		List<ReportBean> tableList=datatablesViewPageBean.getReturnData();
		tableList.forEach(report->{
			String xx="未知";
			if(report.getStatus()==0){
				if(report.getFirstApprovalStatus()==1){
					xx= "已审核,等待二级审核";
				}else{
					xx= "未审核";
				}
			}else if(report.getStatus()==1){
				if(report.getIsLaunch()==1){
					xx= "申请退回";
				}else{
					xx= "已审核";
				}
			}else{
				xx= "已退回";
			}
			report.setStatusStr(xx);
		});
		String [] tableHeadFields={"订单编号","工厂型号","客户型号","加工对象","颜色","车间名称","工序名称","关联源","报工时间","合格数","不合格数","不合格原因","单价","报工人","审核人","状态"};
		String [] fields={"companyOrderId","factoryId","customerId","machining","color","workShopName","procedur","relationSource","touchTime","qualified","noQualified","noQualifiedReason","unitPrice","touchPeople","auditor","statusStr"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		ResponseUtil.downloadfile(response, excelFile, "报工记录.xlsx", true);
	}
	
	
	
	
	/**
	 * 校验是否有可以自己审核自己
	 * type  审批类型
	 * @param request
	 * @return
	 * @throws ServiceException
	 */
	@RequestMapping("/checkSelfApprovalPermission")
	@ResponseBody
	public Map checkSelfApprovalPermission(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(approvalService.checkSelfApprovalPermission(map));
	}
	
	//获取需要二次审核的数据
	@RequestMapping("/getSecondRoportApprovalData")
	@ResponseBody
	public DatatablesViewPageBean getSecondRoportApprovalData(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getSecondRoportApprovalData(map);
	}
	
	//获取本人未审批的报工数据
	@RequestMapping("/getMyRoportData")
	@ResponseBody
	public DatatablesViewPageBean getMyRoportData(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return workshopService.getMyRoportData(map);
	}
	
    //审批报工
	@RequestMapping("/approvalReport")
	@ResponseBody
	public Map approvalReport(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		String data=approvalService.approvalReport(map);
		return ResponseUtil.ResponseResult(data);
	}
	
	//批量审批报工
	@SuppressWarnings("rawtypes")
	@RequestMapping("/allApprovalReport")
	@ResponseBody
	public Map allApprovalReport(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		String data=approvalService.allApprovalReport(map);
		return ResponseUtil.ResponseResult(data);
	} 
	
	//删除报工
	@RequestMapping("/deleteReport")
	@ResponseBody
	public Map deleteReport(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		workshopService.deleteReport(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
	
	//修改报工
//	@RequestMapping("/updateReport")
//	@ResponseBody
//	public Map updateReport(String reportArrayJSONStr,  HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
//		Map map=RequestUtil.getRequestAllParam(request);
//		List<ReportBean> list=workshopService.updateReport(map);
//		return ResponseUtil.ResponseResult(list);
//	}
	
	
	//删除报工
	@RequestMapping("/deleteAllReport")
	@ResponseBody
	public Map deleteAllReport(String idArray,  HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		workshopService.deleteAllReport(idArray);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
	
	//获取审批人员信息
	@RequestMapping("/getApproval")
	@ResponseBody
	public DatatablesViewPageBean getApproval(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return approvalService.getApproval(map);
	}
	
	
//	//点击修改按钮，获取有和没有的用户
//	@RequestMapping("/update")
//	@ResponseBody
//	public Map update(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
//		Map map=RequestUtil.getRequestAllParam(request);
//		return ResponseUtil.ResponseResult(approvalService.update(map));
//	}
	
	@RequestMapping("/updateApproval")
	@ResponseBody
	public Map updateApproval(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		approvalService.updateApproval(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	
	//移除修改
//	@RequestMapping("/updateRemove")
//	@ResponseBody
//	public Map updateRemove(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
//		Map map=RequestUtil.getRequestAllParam(request);
//		approvalService.updateRemove(map);
//		return ResponseUtil.ResponseResult(ReturnStringUtil.REMOVE_SUCCESS);
//	}
	
	//添加修改
//	@RequestMapping("/updateAdd")
//	@ResponseBody
//	public Map updateAdd(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
//		Map map=RequestUtil.getRequestAllParam(request);
//		approvalService.updateAdd(map);
//		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
//	}
	
	//添加新的审批
	@RequestMapping("/addApperval")
	@ResponseBody
	public Map addApperval(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		approvalService.addApperval(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}
	
	//删除审批
	@RequestMapping("/delete")
	@ResponseBody
	public Map delete(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		approvalService.delete(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
	//获取未审批是投料单
	@RequestMapping("/getMyFeedOrderNotApprovalData")
	@ResponseBody
	public Map getMyFeedOrderNotApprovalData(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		map.put("createUser", map.get("loginUserId"));
		List list=new ArrayList();
		list.add(0);
		list.add(2);
		map.put("statusList", list);
		return ResponseUtil.ResponseResult(orderService.getFeedOrderData(map));
	}
	
	
	//修改投料单明细
	@RequestMapping("/updateFeedDetail")
	@ResponseBody
	public Map updateFeedDetail(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		orderService.updateFeedDetail(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	
	//删除投料单明细
	@RequestMapping("/deleteAllFeed")
	@ResponseBody
	public Map deleteAllFeed(String idArray,  HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		orderService.deleteAllFeed(idArray);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
	//获取需要我待审批投料单明细
	@RequestMapping("/getFeedOrderNotApprovalData")
	@ResponseBody
	public Map getFeedOrderNotApprovalData(HttpServletRequest request) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderService.getFeedOrderNotApprovalData(map));
	}
	
	//批量审批投料单
	@RequestMapping("/allApprovalFeedOrder")
	@ResponseBody
	public Map allApprovalFeedOrder(HttpServletRequest request,int  status) throws ServiceException, CloneNotSupportedException{
		Map map=RequestUtil.getRequestAllParam(request);
		String returnStr="";
		if(status==1) {
			returnStr=ReturnStringUtil.APPROVAL_SUCCESS;
		}
		if(status==2) {
			returnStr=ReturnStringUtil.RETURN_SUCCESS;
		}
		if(status ==3) {
			returnStr=ReturnStringUtil.RETURN_SUCCESS;
		} 
		String message = orderService.allApprovalFeedOrder(map);
		return ResponseUtil.ResponseResult(returnStr+"  "+message);
	}
	
	
	/**
	 * 批量审核
	 * @date 2018年5月17日 上午8:43:20 
	 * @author 李斌
	 * @return Map<String,Object> 
	 * @throws
	 */
	@RequestMapping("/setAllPurchaseStatus")
	@ResponseBody
	public Map<String,Object> setAllPurchaseStatus(HttpServletRequest request,int status) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		String returnStr="";
		if(status==1) { 
			returnStr=ReturnStringUtil.APPROVAL_SUCCESS;
		}
		if(status==2) {
			returnStr=ReturnStringUtil.RETURN_SUCCESS;
		}
		String message = PurchaseOrderService.approvalAllPurchaseOrder(map, status);
		return ResponseUtil.ResponseResult(returnStr+"  "+message);
	}
	
	/**
	 * 修改采购单明细表信息
	* @date 2018年4月15日 上午9:42:32 
	* @author 李斌
	* @return Map 
	* @throws
	 */
	@RequestMapping("/updatePurchaseOrder")
	@ResponseBody
	public Map updatePurchaseOrder(HttpServletRequest request) throws ServiceException {
		@SuppressWarnings("rawtypes")
		Map map = RequestUtil.getRequestAllParam(request);
		orderService.updatePurchaseOrder(map);
		return ResponseUtil.ResponseResult("修改成功"); 
	} 
	
	
	
	
	//获取出入库信息
	@RequestMapping("/getInWarehourseNotApprovalData")
	@ResponseBody
	public DatatablesViewPageBean getInWarehourseNotApprovalData(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		return companyMaterialService.getInWarehourseNotApprovalData(map);
	}
	
	
	
	
	//审批出入库
	@RequestMapping("/agreeInWarehourse")
	@ResponseBody
	public Map agreeInWarehourse(HttpServletRequest request,int status) throws ServiceException, CloneNotSupportedException, ParseException{
		Map map=RequestUtil.getRequestAllParam(request);
		String returnStr="";
		if(status==1) {
			returnStr=ReturnStringUtil.APPROVAL_SUCCESS;
		} 
		if(status==2) {
			returnStr=ReturnStringUtil.RETURN_SUCCESS;
		}
		String message = companyMaterialService.agreeInWarehourse(map,status,request);
		if("重新确认".equals(message)) {
			return ResponseUtil.ResponseResult(message);
		}else {
			return ResponseUtil.ResponseResult(returnStr+"  "+message);
		}
		
	}
	
	
	//修改出入库信息信息
	@RequestMapping("/updateWarehouseInOut")
	@ResponseBody
	public Map updateWarehouseInOut(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		companyMaterialService.updateWarehouseInOut(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS);
	}
	
	//删除出入库信息
	@RequestMapping("/deleteWarehouseInOut")
	@ResponseBody
	public Map deleteWarehouseInOut(HttpServletRequest request) throws ServiceException{
		Map map=RequestUtil.getRequestAllParam(request);
		companyMaterialService.deleteWarehouseInOut(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
	/**
	 * @throws CloneNotSupportedException 
	 * 审核出入库信息
	* @date 2018年5月30日 下午4:57:20 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/agreeOtherWarehouse")
	@ResponseBody
	public Map<String,Object> agreeOtherWarehouse(HttpServletRequest request,int status) throws ServiceException, CloneNotSupportedException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		String returnStr="";
		if(status==1) {
			returnStr=ReturnStringUtil.APPROVAL_SUCCESS;
		}
		if(status==2) { 
			returnStr=ReturnStringUtil.RETURN_SUCCESS;
		}
		String message = OtherWarehouseService.updateOtherWarehouseStatusById(map,status);
		return ResponseUtil.ResponseResult(returnStr+"  "+message);
	}
	
	/**
	 * 删除出入库信息
	* @date 2018年5月30日 下午4:57:36 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/deleteOtherWarehouse")
	@ResponseBody
	public Map<String,Object> deleteOtherWarehouse(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		OtherWarehouseService.deleteOtherWarehouseById(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.DELETE_SUCCESS);
	}
	
	/**
	 * 假如前台拦截出了问题. 那么就继续后台拦截
	* @date 2018年5月30日 下午5:14:13 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/updateOtherWarehouse")
	@ResponseBody
	public Map<String,Object> updateOtherWarehouse(HttpServletRequest request) throws ServiceException{
		throw new ServiceException("请至我的报工页面修改");
	}
	@RequestMapping("/updateOtherWarehouseByOrder")
	@ResponseBody
	public Map<String,Object> updateOtherWarehouseByOrder(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		OtherWarehouseService.updateOtherWarehouseByOrder(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.UPDATE_SUCCESS); 
	} 
	
	//获取审批人员信息
//	@RequestMapping("/returnInWarehourse")
//	@ResponseBody
//	public Map returnInWarehourse(HttpServletRequest request) throws ServiceException{
//		Map map=RequestUtil.getRequestAllParam(request);
//		companyMaterialService.returnInWarehourse(map);
//		return ResponseUtil.ResponseResult(ReturnStringUtil.RETURN_SUCCESS);
//	}
	
	/**
	 * Title: addApprovalProcess
	 * Description: 添加考勤审批流程  
	 * @param request
	 * @author 王荣
	 * @date 2018年6月23日 上午10:54:24
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/addApprovalProcess")
	@ResponseBody
	public Map<String,Object> addApprovalProcess(HttpServletRequest request) {
		Map<String,Object> map=RequestUtil.getRequestParamByGeneric(request);
		approvalService.addApprovalProcess(map);
		return ResponseUtil.ResponseResult(ReturnStringUtil.ADD_SUCCESS);
	}

	
	
}
